package nr.nodehost.config;

import java.io.File;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.util.ArrayList;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;

import net.sf.json.JSONObject;
import nr.nodehost.HttpRequestUtils;
import nr.nodehost.NodeHostApplication;
import nr.nodehost.Utils;
import nr.nodehost.model.Node;
import nr.nodehost.service.AsyncTaskService;
import nr.nodehost.service.NodeService;

@Configuration
public class NodeHostConfig {
	@Value("${node.name}")
	private String nodeName;
	@Value("${node.id}")
	private int nodeId;
	@Value("${node.ip}")
	private String nodeIp;
	@Value("${simserver.baseurl}")
	private String serverBaseUrl;
	
	@Autowired
    private AsyncTaskService taskService;
	@Autowired
	private NodeService nodeService;
	//初始化变量

	@Value("${node.Resource.type}")
	private String NodeResourceType;

    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {
    	System.out.println(event);
    	try{
    		System.out.println("node id: "+nodeId);
    		/*
    		String nodeIp="127.0.0.1";
    		ArrayList<String> ipList=Utils.getLocalIpAddress();
    		if(ipList.size()>0)
    			nodeIp = ipList.get(0);
		    System.out.println("node ip: "+nodeIp);
		    */
		    Node node=new Node();
		    node.setId(nodeId);
		    node.setName(nodeName);
		    node.setIp(nodeIp);
		    node.setCpu(4);
		    node.setCpuUsage(10);
		    node.setMemory(8192);
		    node.setMemoryUsage(20);
		    node.setDisk(60*1024);
		    node.setDiskUsage(40);

			node.setResource_type(NodeResourceType);

		    nodeService.update(node);
    		//System.out.println(NodeHostApplication.dispatcher);//.init();
    		/*
    		JSONObject nodeParam=new JSONObject();
    		
	    	nodeParam.put("name", nodeName);
	    	nodeParam.put("totalMemory", 1024*32);
	    	JSONObject result=HttpRequestUtils.httpPost(serverBaseUrl+"/node/register", nodeParam, false);
	    	System.out.println("node register result:"+result);
	    	if(result!=null&&result.getInt("code")==0) {
	    		System.out.println(nodeName+" register success.");
	    	}
	    	else {
	    		System.out.println(nodeName+" register fail.");
	    	}
	    	*/
	    	//taskService.runMonitor();
	    	//taskService.runUdpListener();
	    	//System.out.println("runMonitor finished");
	    	/*
    		File f=new File("/home/wanbo/nr/run/schema1.json");
    		FileInputStream fis=new FileInputStream(f);
    		byte[] content=new byte[(int)f.length()];
    		try{
    			fis.read(content);
    			fis.close();
    			String schemaContent=new String(content);
    			JSONObject schema=new JSONObject();
    			schema.put("id", 1);
    			schema.put("loopNumber", 2);
    			schema.put("content",JSONObject.fromObject(schemaContent));
    			taskService.runSchema(schema);
    			System.out.println("runSchema finished");
    		}
    		catch(Exception e) {
    			e.printStackTrace();
    		}
	    	*/
    	}
    	catch(Exception e) {
    		e.printStackTrace();
    	}
    }

    //Add by guoheng 12-4-2023
	//关闭时从数据库删除节点信息
	@EventListener
	public void onApplicationEventClose(ContextClosedEvent event){
		System.out.println("node host config close");
		nodeService.delete(nodeId);
	}

  //初始化变量
    @PostConstruct
    public void postConstruct(){

    }
}
